﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace FGP_Kpaint
{
    class SwipeGesture
    {
        private MainWindow main;
        private System.ComponentModel.BackgroundWorker mWorker;
        private System.Windows.Threading.DispatcherTimer dispatcherTimer;
        bool collision, delay, status;


        //positions
        int right_hand, head, right_shoulder , left_hand, left_shoulder;


        int lRhand;
            
        
        //list for final gesture 
        ArrayList finalGestureR; //right
        ArrayList finalGestureL; // left

        Gallery gallery;
        Controler controler;


        string res;

        public SwipeGesture(MainWindow mains, Controler controler1)
        {
            
            this.main = mains;
            this.controler = controler1;
            
            gallery = new Gallery(this.main);
            
            collision = true;
            delay = true;
            status = false;
            lRhand = 0;

            res = "";
            right_hand = 0;
            head = 0;
            right_shoulder = 0;
            left_hand = 0;
            left_shoulder = 0;

            finalGestureR = new ArrayList();
            finalGestureL = new ArrayList();
            //progress bar start
            mWorker = new System.ComponentModel.BackgroundWorker();
            mWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(mWorker_DoWork);
            mWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(mWorker_ProgressChanged);
            mWorker.WorkerReportsProgress = true;
            mWorker.WorkerSupportsCancellation = true;
            mWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(mWorker_RunWorkerCompleted);
            //progress bar end

            
            //timer
            dispatcherTimer = new System.Windows.Threading.DispatcherTimer();
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
            //end timer
        }

        public void stopImage() {

            gallery.stopImages();
        }


        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            delay = true;
            status = false;
            finalGestureR.Clear();
            finalGestureL.Clear();
            res = "";
            right_hand = 0;
            head = 0;
            right_shoulder = 0;
            left_hand = 0;
            left_shoulder = 0;
           // main.gestureIcon.Fill = controler.swipeIcon; // resets the swipe icon
            dispatcherTimer.Stop();
        }

        private void mWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
        {
            mWorker.Dispose();
            delay = false;
            status = true;

            //do work
           
            if (finalGestureL.Count < finalGestureR.Count)
            {
                int[] array = finalGestureR.ToArray(typeof(int)) as int[];
                if (finalGestureR.Count == 2)
                {
                    if (array[0] == 1)
                    {
                        main.textBlock4.Text = "right";
                        gallery.getNextImage();


                    }
                    else
                    {
                        main.textBlock4.Text = "failed";
                        stopImage();
                        controler.setFlag_gallery(false);
                    }
                }
                else
                    main.textBlock4.Text = "not completed";
            }
            else if(finalGestureL.Count > finalGestureR.Count) {
                int[] array = { 0, 0 };
                   array =  finalGestureL.ToArray(typeof(int)) as int[];
                   if (finalGestureL.Count == 2)
                   {
                       if (array[0] == 4)
                       {
                           main.textBlock4.Text = "left";
                           gallery.getPreviousImage();
                       }
                       else
                           main.textBlock4.Text = "failed";
                   }
                   else
                       main.textBlock4.Text = "not completed";
            }

            //clearing arrays
            dispatcherTimer.Start();
        }

        private void mWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
        {
            main.progressBar2.Value = e.ProgressPercentage;
        }

        private void mWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            for (int i = 1; i < 100; i++)
            {
                mWorker.ReportProgress(i);

                // Do some part of the work
                System.Threading.Thread.Sleep(20);

                checkSwipeGesture();

                // Check if the user wants to abort
                if (collision == false)
                {
                    e.Cancel = true;
                    return;
                }
            }
            mWorker.ReportProgress(100);  // Done
        }


        public void runWorker()
        {
            if (mWorker != null)
            {
                if (!mWorker.IsBusy)
                {
                    if(delay)
                    mWorker.RunWorkerAsync();
                }
            }
        }


        public void stopWorker()
        {
            if (mWorker != null)
            {
                if (mWorker.IsBusy)
                {
                    mWorker.CancelAsync();
                    main.progressBar2.Value = 0;

                    if (dispatcherTimer != null)
                    {
                        dispatcherTimer.Stop();
                    }
                }
            }
        }

        public void setCollision(bool flag)
        {
            this.collision = flag;
        }

        public void setDelay(bool flag)
        {
            this.delay = flag;
        }

        public bool getCollision()
        {
            return this.collision;
        }

        public bool getDelay()
        {
            return this.delay;
        }

        public bool getStatus()
        {
            return this.status;
        }


        //swipe gestures

        //setters
        public void setRight_Hand(int num) {
            this.right_hand = num;
        }

        public void setHead(int num)
        {
            this.head = num;
        }

        public void setRight_Shoulder(int num)
        {
            this.right_shoulder = num;
        }

        public void setleft_Shoulder(int num)
        {
            this.left_shoulder = num;
        }


        public void setLeft(int num)
        {
            this.left_hand = num;
        }

        public void setLRhand(int num) {

            this.lRhand = num;
        }

        public string checkSwipeGesture() { 
        
            // 1 for right
            // 2 for left
            //3 for middle
            res = "";
            if (lRhand == 1)
            {

                if (right_hand > right_shoulder + 120)
                {
                    if (!finalGestureR.Contains(1))
                    {
                        finalGestureR.Add(1);
                        //main.gestureIcon.Fill = controler.swipeIconR;
                        //res = "1";
                    }
                }
                else if (right_hand < head + 20)
                {
                    if (!finalGestureR.Contains(2))
                    {
                        finalGestureR.Add(2);
                       // main.gestureIcon.Fill = controler.swipeIconL;
                       // res = "2";
                    }
                }
            }
            else if (lRhand == 2)
            {
                if (left_hand + 50 > left_shoulder)
                {
                    if (!finalGestureL.Contains(3))
                    {
                        finalGestureL.Add(3);
                       // main.gestureIcon.Fill = controler.swipeIconR;
                       // res = "3";
                    }
                }
                else if (left_hand < left_shoulder - 80)
                {
                    if (!finalGestureL.Contains(4))
                    {
                        finalGestureL.Add(4);
                       // main.gestureIcon.Fill = controler.swipeIconL;
                       // res = "4";
                    }
                }
            }

            return res;
        }

        public string getRes() {
            return this.res;
        }
    }
}
